package com.cx330;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;

public class Example63 {
    @Test
    public void test1() {
        HashSet<String> set = new HashSet<>();
        set.add("cx330");
        set.add("java");
        set.add("hello");
        set.add("hello");
        set.add("hello");
        System.out.println(set);
    }

    @Test
    public void test2() {
        LinkedHashSet<String> set = new LinkedHashSet<>();
        set.add("cx330");
        set.add("java");
        set.add("hello");
        set.add("hello");
        set.add("hello");
        System.out.println(set);
    }

    @Test
    public void test3() {
        TreeSet<String> set = new TreeSet<>();
        set.add("cx330");
        set.add("java");
        set.add("hello");
        set.add("hello");
        set.add("hello");
        System.out.println(set);
    }

    @Test
    public void test4() {
        TreeSet<Employee> set = new TreeSet<>();
        set.add(new Employee(2, "张三", 15000));
        set.add(new Employee(1, "李四", 16000));
        set.add(new Employee(3, "王五", 13000));
        for (Employee e : set) {
            System.out.println(e);
        }
    }

    @Test
    public void test5() {
        Comparator<Employee> c = new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                return Double.compare(o1.getSalary(), o2.getSalary());
            }
        };
        TreeSet<Employee> set = new TreeSet<>(c);
        set.add(new Employee(2, "张三", 15000));
        set.add(new Employee(1, "李四", 16000));
        set.add(new Employee(3, "王五", 13000));
        for (Employee e : set) {
            System.out.println(e);
        }
    }

    @Test
    public void test6() {
        TreeSet<Employee> set = new TreeSet<>(Comparator.comparingDouble(Employee::getSalary));
        set.add(new Employee(2, "张三", 15000));
        set.add(new Employee(1, "李四", 16000));
        set.add(new Employee(3, "王五", 13000));
        for (Employee e : set) {
            System.out.println(e);
        }
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Employee implements Comparable<Employee> {
    private int id;
    private String name;
    private double salary;

    @Override
    public int compareTo(Employee o) {
        return this.id - o.id;
    }
}